package edu.xpu.project.common.task;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import edu.xpu.project.record.entity.AssignBusRecord;
import edu.xpu.project.record.service.AssignBusRecordService;
import edu.xpu.project.vehicle.entity.BusSchedule;
import edu.xpu.project.vehicle.entity.SchoolBus;
import edu.xpu.project.vehicle.service.SchoolBusService;

/**
 * @ClassName: AssignRecordOverTask.java
 * @Description: 派遣记录完成定时器
 *
 * @author liukang
 * @date 2017年5月26日 下午3:53:24
 * @version 
 * @since JDK 1.6
 */
public class AssignRecordOverTask {
	@Autowired
	private AssignBusRecordService assignBusRecordService;
	@Autowired
	private SchoolBusService schoolBusService;
	/**
	  * @MethodName: checkAssignRecord
	  * @Description: 该定时任务30分钟执行一次
	  * 		对于按照调度表，应该完成的派遣记录进行更新
	  * @Author liukang
	  * @date 2017年5月26日  下午4:37:49
	 */
	public void checkAssignRecord() {
		assignRecordsUpdate();
	}
	
	//对于以发车的进行更新
	public void assignRecordsUpdate() {
		List<AssignBusRecord> recordsByStatus = assignBusRecordService.getRecordsByStatus(2);
		for (AssignBusRecord assignBusRecord : recordsByStatus) {
			//根据时间进行判断
			BusSchedule busSchedule = assignBusRecord.getBusSchedule();
			Date endTime = busSchedule.getEndTime();
			if (isOverEndDate(endTime)) {	//如果超过当前时间, 就去完成发车
				String schoolBusIds = assignBusRecord.getSchoolBusIds();
				String[] busIds = schoolBusIds.split(",");
				for (int i = 0, len = busIds.length; i < len; i++) {
					SchoolBus schoolBus = schoolBusService.get(Long.parseLong(busIds[i]));
					if (schoolBus != null) {
						schoolBus.setStatus(1);		//已发车完成
						//设置校车的起点和终点站（应该有变化）
						BusSchedule schedule = assignBusRecord.getBusSchedule();
						Integer endStation = schedule.getEndStation();
						//重置当前位置
						schoolBus.setLocalStatus(endStation);
						schoolBusService.saveOrUpdate(schoolBus);
					}
				}
				
			}
			System.out.println();
			
			/*assignBusRecord.setStatus(3);	//发车完成
			AssignBusRecord saveOrUpdate = assignBusRecordService.saveOrUpdate(assignBusRecord);
			if (saveOrUpdate != null) {
				
			}*/
		}
	}
	
	public static boolean isOverEndDate(Date endDate) {
        Calendar nowDate = Calendar.getInstance();
        int nowHour = nowDate.get(Calendar.HOUR_OF_DAY);
        int nowMinute = nowDate.get(Calendar.MINUTE);
        int nowMilli = nowDate.get(Calendar.MILLISECOND);
        
        Calendar instance = Calendar.getInstance();
        instance.setTime(endDate);
        int endHour = instance.get(Calendar.HOUR);
        int endMinute = instance.get(Calendar.MINUTE);
        int endMilli = instance.get(Calendar.MILLISECOND);
        
        if (nowHour == endHour) {
        	if (nowMinute == endMinute) {
        		if (nowMilli > endMilli) {
        			return true;
        		} else {
        			return false;
        		}
        	} else if (nowMinute > endMinute) {
        		return true;
        	} else {
        		return false;
        	}
        } else if(nowHour > endHour) {		//如果当前时间大于传入的时间, 肯定超过当前日期
        	return true;
        } else {
        	return false;
        }
        
        //nowDate.set(Calendar.MINUTE, 47);
	}

}

